/*
 * Copyright (c) 2009, Takashi Ito
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. Neither the name of the authors nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

#include "sys/queue.h"
#include "tssp.h"

#ifndef _TSSP_TABLE_H_
#define _TSSP_TABLE_H_

#ifdef __cplusplus
extern "C" {
#endif /* __cplusplus */

struct tssp_table_monitor;

typedef void (*tssp_table_monitor_callback)(struct tssp_table_monitor *mon,
                                            struct tssp_section *section,
                                            void *userdata);

struct tssp_table_monitor {
    struct tssp *parser;
    struct tssp_section_listener *listener;
    struct tssp_section *sections[256];

    uint8_t discontinuity;
    uint8_t filled;
    uint8_t last_number;
    uint8_t version;

    uint16_t extension;
    uint16_t mask;
    uint16_t value;

    tssp_table_monitor_callback callback;
    void *userdata;
};

struct tssp_table_payload {
    uint32_t length;
    uint8_t *start;
    uint8_t *end;
    uint8_t *heap;
};

#define TSSP_PAYLOAD_DEEP       (1 << 0)
#define TSSP_PAYLOAD_RAW        (1 << 1)

int tssp_table_get_payload(struct tssp_table_payload *payload,
                           const struct tssp_section *section,
                           int flags);

#define tssp_table_free_payload(payload)    free((payload)->heap)

struct tssp_table_monitor *
tssp_table_monitor_attach(struct tssp *parser,
                          struct tssp_section_filter *filter,
                          tssp_table_monitor_callback callback,
                          void *userdata);
void tssp_table_monitor_detach(struct tssp_table_monitor *monitor);
void tssp_table_monitor_reset(struct tssp_table_monitor *monitor);
void tssp_table_monitor_set_mask(struct tssp_table_monitor *monitor,
                                 uint16_t mask,
                                 uint16_t value);

#ifdef __cplusplus
};
#endif /* __cplusplus */

#endif /* _TSSP_TABLE_H_ */

